package com.intel.factorytest.activity;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.media.AudioManager;
import android.media.AudioSystem;

import com.intel.factorytest.R;
import com.intel.factorytest.log.LogWriter;
import com.pekall.fmradio.FMAudioInterface;
import com.pekall.fmradio.FMRadioService;
import com.pekall.fmradio.IFMRadioService;
import com.pekall.fmradio.IRemoteServiceCallback;

public class FMRadio extends BaseActivity {
    private static final String TAG = "FMRadio";
    private static final String TEST_ITEM = "test_item";

    private boolean mIsFMRadioEnabled = false; // FM radio power on or not
    private static final int FREQUENCY_ADJUST_STEP = 100;
    public static final int LOW_FREQUENCY = 87500;
    public static final int HIGH_FREQUENCY = 108000;
    private static final int LIGHT_ON_TIME = 40000; // 40s light on when scan
    private static final int LONG_PRESS_TUNE_TIMEOUT = 100;
    private static final int LONG_PRESS_SEEK_TIMEOUT = 2000;
    
    
    private static final int FM_SEEK_MAX_CHANNEL = 3;
    private static final int FM_SEEK_THRESHOLD = 6;
    private static  int fm_channel_num = 0;

    private static final String SYSTEM_FM_PACKAGE="com.intel.fm.activities";
    private static final String SYSTEM_FM_CLASS="com.intel.fm.activies.MainActivity";
    private static final String SYSTEM_FMR_PACKAGE="com.caf.fmradio";//"com.pekall.fmradio";
    private static final String SYSTEM_FMR_CLASS="com.caf.fmradio.FMRadio";//com.pekall.fmradio.FMRadio";
    private static final String SYSTEM_TEST_START_FM_MODE="com.caf.fmr.fm.startmode";
    private boolean mFMRadioStarted = false;

    public static int mCurrentFreq = LOW_FREQUENCY;
    
    private TextView mFMHeadsetNote;
    private TextView mFMFrepTextView;
    
    private boolean bStartToFM = false;

    private AudioManager mAudioManager;
    int mVolumeTemp;

    String textString = "";
    private BroadcastReceiver headsetReceiver = null;
    Intent mInitiateIntent;
    private boolean bindSuccessed;
    private boolean isStartSystemFM;

    private Button mSucBtn;
    public static boolean mFirstRun = true;
    public static boolean isWiredHeadsetOn() {
        if (AudioSystem.getDeviceConnectionState(
                AudioSystem.DEVICE_OUT_WIRED_HEADSET, "") == AudioSystem.DEVICE_STATE_AVAILABLE) {
            return true;
        }
        if (AudioSystem.getDeviceConnectionState(
                AudioSystem.DEVICE_OUT_WIRED_HEADPHONE, "") == AudioSystem.DEVICE_STATE_AVAILABLE) {
            return true;
        }
        return false;
    }

    private boolean bindToService() {
        Log.i(TAG, "Start to bind to FMRadio service");
        Intent i = new Intent();
        i.setComponent(new ComponentName("com.pekall.fmradio",
                "com.pekall.fmradio.FMRadioService"));
        return bindService(i,// "com.pekall.fmradio.FMRADIO_SERVICE"
                mServConnection, Context.BIND_AUTO_CREATE);
    }
    private boolean bindToService2() {
        Log.i(TAG, "Start to bind to FMRadio service");
        Intent i = new Intent();
        i.setComponent(new ComponentName("com.pekall.fmradio",
                "com.pekall.fmradio.FMRadioService"));
        return bindService(i,// "com.pekall.fmradio.FMRADIO_SERVICE"
                mServConnection, Context.BIND_AUTO_CREATE);
    }

    private void unbindFMRadioService() {
        if (mIsBound) {
            unbindService(mServConnection);
            mIsBound = false;
        }
    }

    private IFMRadioService mFMService = null;
    private boolean mIsBound = false;
    private ServiceConnection mServConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className,
                android.os.IBinder service) {
            Log.w(TAG, "onServiceConnected");
            mIsBound = true;

            mFMService = IFMRadioService.Stub.asInterface(service);
            if (mFMService == null) {
                Log.e(TAG, "onServiceConnected error, mFMService null");
                return;
            }

            updateTrackInfo();

            try {
                mFMService.registerCallback(mCallback);
            } catch (RemoteException e) {
                Log.e(TAG, "", e);
            }
        }

        public void onServiceDisconnected(ComponentName className) {
            Log.i(TAG, "onServiceDisconnected");
            if (mFMService == null) {
                Log.e(TAG, "onServiceDisconnected error, mFMService null");
                return;
            }

            try {
                mFMService.unregisterCallback(mCallback);
            } catch (RemoteException e) {
                Log.e(TAG, "", e);
            }

            mFMService = null;
            finish();
        }
    };

    private EditText mFrequency;
    private Button mTuneBtn;

    private void initFmContent() {
        isStartSystemFM = false;
        setContentView(R.layout.fm);
        super.initView();
        bStartToFM = false;
        mFrequency = (EditText) findViewById(R.id.fm_frequency);
        mFrequency.setText("94.2");
        mTuneBtn = (Button) findViewById(R.id.fm_tune_btn);
        
        mFMFrepTextView= (TextView) findViewById(R.id.fm_frep);
        mFMHeadsetNote= (TextView) findViewById(R.id.fm_headset_note);

        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        // mHeadsetStatusImgView = (ImageView) findViewById(R.id.headset_icon);
        // mHeadsetRadioBtn = (RadioButton) findViewById(R.id.headset_icon);

        int maxVolume = mAudioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        mVolumeTemp = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);

        fm_channel_num = 0;
        //updateHeadsetStatus();
        if (isWiredHeadsetOn()) {
            mFMHeadsetNote.setText(R.string.SERVICEMENU_FM_INFO_HAVE_HEADSET);
            mFMHeadsetNote.setTextColor(Color.WHITE);
        } else {
            mFMHeadsetNote.setText(R.string.SERVICEMENU_FM_INFO_NO_HEADSET);
            mFMHeadsetNote.setTextColor(Color.RED);
        }
        
        headsetReceiver = new HeadsetStatusChangedReceiver();
        IntentFilter filter = new IntentFilter(headsetAction);
        this.registerReceiver(headsetReceiver, filter);

         if (isWiredHeadsetOn()&& (bStartToFM==false))
         {
                bStartToFM = true;
                fm_channel_num=0;
                mFmHandler.removeMessages(START_FMRADIO);
                mFmHandler.removeMessages(SEEK_NEXT);
                mHandler.removeMessages(FM_SEEK_SUCCEED);
                mHandler.removeMessages(SEEK_NEXT);
                mFmHandler.sendEmptyMessageDelayed(START_FMRADIO, 1500);
//                mFmHandler.sendEmptyMessage(START_FMRADIO);
         }
        mFrequency.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mFrequency.requestFocus();
            }
        });
        mTuneBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mCurrentFreq = (int) (Float.parseFloat(mFrequency.getText()
                        .toString()) * 1000);
                powerOnFMRadio();
                setFMRadioFreq();
            }
        });
    }

    private void initSystemFmContent() {
        setContentView(R.layout.systemfmradio);
        super.initView();
        mInitiateIntent = getSystemIntent();
        mInitiateIntent.putExtra(SYSTEM_TEST_START_FM_MODE, "factorytest");         
        Button startButton = (Button) findViewById(R.id.system_fm_start);
        startButton.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                isStartSystemFM = true;
                Log.d("LJK", "mInitiateIntent = "+mInitiateIntent);
                //startActivity(mInitiateIntent);
                startActivityForResult(mInitiateIntent, 0);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG,"onActivityResult >>>> requestCode:" + requestCode + ",resultCode:" + resultCode);
        switch (resultCode) {
           case 0: 
           {
                Log.d(TAG, "onActivityResult mFirstRun = false");
                mFirstRun = false;
                //mSucBtn.setVisibility(View.VISIBLE);
           }
           break;
        default:
            break;
        }
    }

    private Intent getSystemIntent() {
/*        PackageManager packageManager = this.getPackageManager();
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        try {
            PackageInfo info = packageManager.getPackageInfo(SYSTEM_FM_PACKAGE, PackageManager.GET_ACTIVITIES);
            resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            resolveIntent.setPackage(info.packageName);
        } catch (Exception e) {
            Log.e(TAG, "this package name not installed");
        }

        List<ResolveInfo> apps = packageManager.queryIntentActivities(
                resolveIntent, 0);
//        for (int i = 0;i < apps.size(); i++ ) {
//            ResolveInfo ri = apps.get(i);
//            Log.d("LJK", "packageName = "+ri.activityInfo.packageName+"; calssName = "+ri.activityInfo.name);
//            
//        }

        ResolveInfo ri = apps.iterator().next();

         if (ri != null ) { 
             String className = ri.activityInfo.name;
             String packageName = ri.activityInfo.packageName;

             Intent intent = new Intent(Intent.ACTION_MAIN); 
             intent.addCategory(Intent.CATEGORY_LAUNCHER); 

             ComponentName cn = new ComponentName(packageName, className); 

             intent.setComponent(cn);
             return intent;
         } else */{
             Intent intent = new Intent(Intent.ACTION_MAIN); 
             intent.addCategory(Intent.CATEGORY_LAUNCHER);
             ComponentName cn = new ComponentName(SYSTEM_FMR_PACKAGE, SYSTEM_FMR_CLASS);
             intent.setComponent(cn);
             return intent;
         }
    }

    public void onCreate(Bundle b) {
        super.onCreate(b);
//        updateConfiguration();
        Log.d(TAG, "onCreate");

        bindSuccessed = bindToService();
        if (bindSuccessed) {
            initFmContent();
        } else {
            initSystemFmContent();
        }
        
        mSucBtn = (Button) findViewById(R.id.success_button);        
        mSucBtn.setVisibility(View.INVISIBLE);
    }
    
    private void updateHeadsetStatus(Intent intent) {
        if (mFMHeadsetNote == null) {
            return;
        }
        if (intent.hasExtra("state")) {
            if (intent.getIntExtra("state", 0) == 0) {
                Log.d(TAG, "headset out");
                powerOffFMRadio();
                mFMHeadsetNote.setText(R.string.SERVICEMENU_FM_INFO_NO_HEADSET);
                mFMHeadsetNote.setTextColor(Color.RED);
            } else {
                Log.d(TAG, "headset insert");
                mFMHeadsetNote
                        .setText(R.string.SERVICEMENU_FM_INFO_HAVE_HEADSET);
                mFMHeadsetNote.setTextColor(Color.WHITE);
                //if (fm_channel_num !=3)
                if (bStartToFM==false)
                {
                    bStartToFM = true;
//                    bindToService();
                    fm_channel_num=0;
//                    textString="";
                    mFmHandler.removeMessages(START_FMRADIO);
                    mFmHandler.removeMessages(SEEK_NEXT);
                    mHandler.removeMessages(FM_SEEK_SUCCEED);
                    mHandler.removeMessages(SEEK_NEXT);
//                    mFmHandler.sendEmptyMessageDelayed(START_FMRADIO, 1500);
                    mFmHandler.sendEmptyMessage(START_FMRADIO);
                }
                
            }
        }
//        if (mAudioManager.isWiredHeadsetOn()) {
//            mFMHeadsetNote.setText(R.string.SERVICEMENU_FM_INFO_HAVE_HEADSET);
//            mFMHeadsetNote.setTextColor(Color.WHITE);
//            if (fm_channel_num !=3)
//            {
//                fm_channel_num=0;
//                mFmHandler.sendEmptyMessageDelayed(START_FMRADIO, 500);
//            }
//        } else {
//            mFMHeadsetNote.setText(R.string.SERVICEMENU_FM_INFO_NO_HEADSET);
//            mFMHeadsetNote.setTextColor(Color.RED);
//        }
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        Log.d(TAG, "onStart");
        super.onStart();
        // powerOnFMRadio();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.d(TAG, "onResume");
        SharedPreferences fmShared = this.getSharedPreferences("fm_shared", Context.MODE_PRIVATE);
        isStartSystemFM = fmShared.getBoolean("isStartSystemFM", isStartSystemFM);
        if (isStartSystemFM) {
            isStartSystemFM = false;
        }
        Log.d(TAG, "onResume mFirstRun = " + mFirstRun);
        if(!mFirstRun) {
            mSucBtn.setVisibility(View.VISIBLE);
        }

        //powerOnFMRadio();
        // mCurrentFreq = LOW_FREQUENCY;
        // setFMRadioFreq();
//        if (isWiredHeadsetOn())
//        {
//            mFmHandler.sendEmptyMessageDelayed(START_FMRADIO, 1000);
//        }
        // setFMRadioThreshold(FM_SEEK_THRESHOLD);
        mItemTestFinish = true;//add by wangsm
    }

//    protected void setTestResult() {
//        Intent data = new Intent();
//        data.putExtra(TEST_ITEM, test_pos);
//        data.putExtra(TEST_ITEM_RESULT, is_pass);
//        setResult(0, data);
//    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        SharedPreferences fmShared = this.getSharedPreferences("fm_shared", Context.MODE_PRIVATE);
        Editor iditEditor = fmShared.edit();
        iditEditor.putBoolean("isStartSystemFM", isStartSystemFM);
        iditEditor.commit();
        //powerOffFMRadio();
        if (mAudioManager != null)
        mAudioManager
                .setStreamVolume(AudioManager.STREAM_MUSIC, mVolumeTemp, 0);
    }

    @Override
    public void finish() {
        setTestResult();
        mFirstRun = true;
        Log.d(TAG, "finish");
        if (bindSuccessed) {
        unregisterReceiver(headsetReceiver);
        }
        super.finish();
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "onDestroy");
        unbindFMRadioService();
        bStartToFM = false;
        super.onDestroy();
    }

    final Handler mFmHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Log.d(TAG, "mFmHandler msg.what:"+msg.what);
            switch (msg.what) {
            case START_FMRADIO:
                int ret = getFMServiceStatus();
                Log.i(TAG, "getFMServiceStatus() = " + ret);
                if (ret != FMRadioService.FM_SERVICE_ON) {
                    Log.i(TAG, "service state is not READY."
                            + "Will call powerOnFMRadio() now");
                    // powerOnFMRadio();
                } else {
                    // mIsFMRadioEnabled = true;
                    // enableUI(true, true);
                }
//                mCurrentFreq = (int) (Float.parseFloat(mFrequency.getText()
//                        .toString()) * 1000);
                powerOnFMRadio();
                setFMRadioFreq();
                powerOnFMRadio();
                mCurrentFreq = LOW_FREQUENCY;
                setFMRadioFreq();
                setFMRadioThreshold(FM_SEEK_THRESHOLD);
                seekFMRadioStation(1);
                
                 Message seekNextMsg = mFmHandler.obtainMessage(SEEK_NEXT);
                 long playoutTime = SystemClock.uptimeMillis() + 1500;
                 mFmHandler.sendMessageAtTime(seekNextMsg, playoutTime);
                break;    
                
            case SEEK_NEXT:
                seekFMRadioStation(1);
                break;
                
            default:
                break;
            }
        }
    };

    private void updateTrackInfo() {
        try {
            if (mFMService != null) {
                int freq = mFMService.getFMFreq();
                if (checkFMFreq(freq)) {
                    Log.i(TAG, "updateTrackInfo(): freq = " + freq);
                    mCurrentFreq = freq;
                }
                mIsFMRadioEnabled = (mFMService.getFMServiceStatus() == FMRadioService.FM_SERVICE_ON);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    static boolean checkFMFreq(int freq) {
        return (freq != -1 && freq >= LOW_FREQUENCY && freq <= HIGH_FREQUENCY);
    }

    private static final int START_FMRADIO = 1;
    private static final int STOP_FMRADIO = 2;
    private static final int QUIT = 3;
    private static final int FM_OPEN_FAILED = 4;
    private static final int FM_OPEN_SUCCEED = 5;
    private static final int FM_SETFREQ_SUCCEED = 6;
    private static final int FM_SEEK_SUCCEED = 7;
    private static final int FM_SEEK_FAILED = 8;
    private static final int SEEK_NEXT = 12;
    private static final int FM_FREQUENCY_ADD = 13;
    private static final int FM_FREQUENCY_REDUCE = 14;
    private static final int FM_SEEK_BACKWARD_LONG_PRESS = 15;
    private static final int FM_SEEK_FORWARD_LONG_PRESS = 16;
    static final int FM_START_RECORD = 20;
    static final int FM_RECORD_BEGIN = 21;
    static final int FM_RECORD_END = 26;
    private static final int FM_DELAY_FINISH = 23;
    private static final int FM_SETCHANNEL_ID = 24;
    private static final int FM_INIT_SETCHANNEL_ID = 25;
    private static final int FM_TOGGLE_STATE = 27;
    private static final int FM_CLOSED = 28;

    private static final int FM_DELAY_FINISH_TIMEOUT = 400; // 400ms

    final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Log.d(TAG, "msg.what:"+msg.what);
            switch (msg.what) {
            case FM_OPEN_SUCCEED:

                break;
            case FM_OPEN_FAILED:

                break;
            case FM_SETFREQ_SUCCEED:

                break;
            case FM_SEEK_SUCCEED:
                fm_channel_num++;
                if (fm_channel_num < FM_SEEK_MAX_CHANNEL) {
                    Message seekNextMsg = mHandler.obtainMessage(SEEK_NEXT);
                    long playoutTime = SystemClock.uptimeMillis() + 1500;
                    mHandler.sendMessageAtTime(seekNextMsg, playoutTime);
                }

                textString += "CH" + Integer.toString(fm_channel_num)+":"
                        + Integer.toString(mCurrentFreq) + "   ";
                mFMFrepTextView.setText(textString);

                if (fm_channel_num == FM_SEEK_MAX_CHANNEL) {
                    if (isSystemTest) {
                        is_pass = true;
                        finish();
                    }
                }

                break;
            case FM_SEEK_FAILED:

                break;
                
             case SEEK_NEXT:
                 seekFMRadioStation(1);
                 break;
            default:
                break;
            }

        }
    };

    private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() {
        public void openFMRadioCallBack(boolean result) {
            Log.i(TAG, "openFMRadioCallBack: " + result);
            if (result) {
                mHandler.sendEmptyMessage(FM_OPEN_SUCCEED);
            } else {
                mHandler.sendEmptyMessage(FM_OPEN_FAILED);
            }
        }

        public void openFMRadioTxCallBack(boolean result) {
        }

        public void setFrequencyCallback(boolean result, int freq) {
            Log.i(TAG, "setFrequencyCallback: " + result + ", " + freq);
            mCurrentFreq = freq;
            mHandler.sendEmptyMessage(FM_SETFREQ_SUCCEED);
        }

        public void seekStationCallback(boolean result, int frequency) {
            Log.i(TAG, "seekStationCallback: " + result + ", " + frequency);
            if (result) {
                mCurrentFreq = frequency;
                mHandler.sendEmptyMessage(FM_SEEK_SUCCEED);
            } else {
                mHandler.sendEmptyMessage(FM_SEEK_FAILED);
            }
        }
    };

    private void powerOnFMRadio() {
        Log.i(TAG, "powerOnFMRadio");
//        System.out.println("tangjun: try to start FM...");
        if (mFMService != null) {
            try {
                if (!mFMRadioStarted) {
                    Log.i(TAG, "mService.open() called!");
                    if (mFMService.openFMRadio()) {
                        mFMRadioStarted = true;
                        System.out.println("tangjun: FM Started!!!");
                    } else {
                        System.out.println("tangjun: FM Start Fail!");
                    }
                }
            } catch (RemoteException ex) {
                Log.i(TAG, "mService.open() RemoteException!");
            }
        }
    }

    private void powerOffFMRadio() {
        Log.i(TAG, "powerOffFMRadio");
        if (mFMService != null) {
            if (mFMRadioStarted) {
                try {
                    Log.i(TAG, "mService.close() called!");
                    disableRds();
                    mFMService.closeFMRadio();
                    mIsFMRadioEnabled = false;
                    mFMRadioStarted = false;
                    /*
                     * fix UI interface display err when switch to spk, add by
                     * RC
                     */
//                    Intent iHeadset = new Intent(
//                            FMAudioInterface.FM_ROUTE_HEADSET);
                    // sendBroadcast(iHeadset);
                    /*
                     * fix UI interface display err when switch to spk, add by
                     * RC
                     */
                } catch (RemoteException ex) {
                    Log.i(TAG, "mService.close() RemoteException!");
                }
            }
        }
    }

    private void disableRds() {
        Log.i(TAG, "disableRds");
        if (mFMService != null) {
            try {
                if (mFMService.getFMServiceStatus() == FMRadioService.FM_SERVICE_ON) {
                    mFMService.disableRds();
                    Log.i(TAG, "disableRds done!");
                }
            } catch (RemoteException e) {
                Log.e(TAG, "disableRds()", e);
            }
        }
    }

    private void setFMRadioFreq() {
        Log.i(TAG, "setFMRadioFreq");
        if (mFMService != null) {
            try {
                mFMService.setFMFreq(mCurrentFreq);
            } catch (RemoteException ex) {
                Log.i(TAG, "In setFMRadioFreq(): RemoteException.!");
            }
        }
    }

    private void setFMRadioThreshold(int value) {
        Log.i(TAG, "setFMRadioThreshold");
        if (mFMService != null) {
            try {
                mFMService.setFMRssiThreshold(value);
            } catch (RemoteException ex) {
                Log.i(TAG, "In setFMRadioThreshold(): RemoteException.!");
            }
        }
    }

    private void seekFMRadioStation(int direction) {
        Log.i("FMRadio", "seekFMRadioStation");
        if (mFMService != null) {
            try {
                mFMService.seekFM(direction);
            } catch (RemoteException ex) {
                Log.i(TAG, "In seekFMRadioStation(): RemoteException.!");
            }
        }
    }

    private int getFMServiceStatus() {
        int ret = FMRadioService.FM_SERVICE_NONE;
        if (mFMService != null) {
            try {
                ret = mFMService.getFMServiceStatus();
            } catch (RemoteException e) {
                Log.e(TAG, "get fmradio service status failed");
            }
        }
        return ret;
    }

    private String headsetAction = "android.intent.action.HEADSET_PLUG";

    class HeadsetStatusChangedReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action = intent.getAction();
            Log.d(TAG, "HeadsetStatusChangedReceiver:"+action);
            if (action.equals(headsetAction)) {
                updateHeadsetStatus(intent);
            }
        }
    }
}
